#pragma once
#include<iostream>
#include<vector>
using namespace std;
template<class K>
struct HashFunc
{
	size_t operator()(const K& key)
	{
		return (size_t)key;
	}
};
template<>
struct HashFunc<string>
{
	size_t operator()(const string& key)
	{
		size_t hash = 0;
		for (auto& e : key)
		{
			hash *= 131;
			hash += e;
		}
		return hash;
	}
};
namespace Hash_Bucket
{
	template<class T>
	struct HashNode
	{
		T _data;
		HashNode<T>* _next;
		HashNode(const T& data)
			:_data(data),
			_next(nullptr)
		{
		}
	};

	// 前置声明
	template<class K, class T, class KeyOfT, class Hash>
	class HashTable;

	template<class K,class T, class Ref,class Ptr,class KeyOfT, class Hash>
	struct __HashTable_Iterator
	{
		typedef HashNode<T> Node;
		typedef __HashTable_Iterator<K, T, Ref, Ptr, KeyOfT, Hash> Self;
		KeyOfT kot;
		Hash hs;
		__HashTable_Iterator(Node* node, const HashTable<K, T, KeyOfT, Hash>* ht)
			:_node(node),
			_ht(ht)
		{ }
		Ref operator*()
		{
			return _node->_data;
		}
		Ptr operator->()
		{
			return &_node->_data;
		}
		bool operator==(const Self& it)
		{
			return _node == it._node;
		}
		bool operator!=(const Self& it)
		{
			return _node != it._node;
		}
		Self& operator++()
		{
			if (_node->_next)
			{
				_node = _node->_next;
			}
			else
			{
				size_t hashi = hs(kot(_node->_data)) % _ht->_tables.size();
				++hashi;
				while (hashi < _ht->_tables.size())
				{
					if (_ht->_tables[hashi])
					{
						break;
					}
					++hashi;
				}
				if (hashi == _ht->_tables.size()) _node = nullptr;
				else _node = _ht->_tables[hashi];
			}
			return *this;
		}
		Self operator++(int)
		{
			Self tmp = *this;
			++(*this);
			return tmp;
		}
		Node* _node;
		const HashTable<K, T, KeyOfT, Hash>* _ht;
	};
	template<class K, class T, class KeyOfT, class Hash>
	class HashTable
	{
	public:
		// 友元声明
		template<class K, class T, class Ref, class Ptr, class KeyOfT, class Hash>
		friend struct __HashTable_Iterator;

		typedef HashNode<T> Node;
		typedef __HashTable_Iterator<K, T, T&, T*, KeyOfT, Hash> Iterator;
		typedef __HashTable_Iterator<K, T, const T&, const T*, KeyOfT, Hash> ConstIterator;
		KeyOfT kot;
		inline unsigned long __stl_next_prime(unsigned long n)
		{
			// Note: assumes long is at least 32 bits.
			static const int __stl_num_primes = 28;
			static const unsigned long __stl_prime_list[__stl_num_primes] =
			{
				53, 97, 193, 389, 769,
				1543, 3079, 6151, 12289, 24593,
				49157, 98317, 196613, 393241, 786433,
				1572869, 3145739, 6291469, 12582917, 25165843,
				50331653, 100663319, 201326611, 402653189, 805306457,
				1610612741, 3221225473, 4294967291
			};
			const unsigned long* first = __stl_prime_list;
			const unsigned long* last = __stl_prime_list +
				__stl_num_primes;
			const unsigned long* pos = lower_bound(first, last, n);
			return pos == last ? *(last - 1) : *pos;
		}
		HashTable()
		{
			_tables.resize(__stl_next_prime(0));
		}
		HashTable(const HashTable& x)
		{
			_tables.resize(__stl_next_prime(0));
			ConstIterator it = x.Begin();
			while (it != x.End())
			{
				Insert(*it);
				++it;
			}
		}
		HashTable& operator=(HashTable x)
		{
			_tables.swap(x._tables);
			_n = x._n;
			return *this;
		}
		~HashTable()
		{
			for (size_t i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				while (cur)
				{
					Node* next = cur->_next;
					delete cur;
					cur = next;
				}
				_tables[i] = nullptr;
			}
		}
		Iterator Begin()
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				if (_tables[i])
				{
					return Iterator(_tables[i], this);
				}
			}
			return End();
		}
		Iterator End()
		{
			return Iterator(nullptr, this);
		}
		ConstIterator Begin() const
		{
			for (int i = 0; i < _tables.size(); i++)
			{
				if (_tables[i])
				{
					return ConstIterator(_tables[i], this);
				}
			}
			return End();
		}
		ConstIterator End() const
		{
			return ConstIterator(nullptr, this);
		}
		pair<Iterator,bool> Insert(const T& data)
		{
			Iterator it = Find(kot(data));
			if (it != End())
				return { it,false };

			Hash hs;
			if (_n == _tables.size())
			{
				//扩容
				size_t newsize = __stl_next_prime(_tables.size() + 1);
				vector<Node*> newtables(newsize, nullptr);
				// 将旧表的数据移动到新表
				for (size_t i = 0; i < _tables.size(); i++)
				{
					Node* cur = _tables[i];
					while (cur)
					{
						Node* next = cur->_next;
						size_t hashi = hs(kot(cur->_data)) % newtables.size();
						cur->_next = newtables[hashi];
						newtables[hashi] = cur;
						cur = next;
					}
					_tables[i] = nullptr;
				}
				_tables.swap(newtables);
			}

			size_t hashi = hs(kot(data)) % _tables.size();
			Node* newnode = new Node(data);
			newnode->_next = _tables[hashi];
			_tables[hashi] = newnode;
			++_n;
			return { Iterator(newnode,this),true };
		}
		Iterator Find(const K& key)
		{
			Hash hs;
			size_t hashi = hs(key) % _tables.size();
			Node* cur = _tables[hashi];
			while (cur)
			{
				if (kot(cur->_data) == key)
				{
					return Iterator(cur,this);
				}
				else
				{
					cur = cur->_next;
				}
			}
			return Iterator(nullptr, this);
		}
		bool Erase(const K& key)
		{
			Hash hs;
			size_t hashi = hs(key) % _tables.size();
			Node* cur = _tables[hashi];
			Node* prev = nullptr;
			while (cur)
			{
				Node* next = cur->_next;
				if (kot(cur->_data) == key)
				{
					if (prev == nullptr)
					{
						_tables[hashi] = next;
					}
					else
					{
						prev->_next = next;
					}
					delete cur;
					--_n;
					return true;
				}
				prev = cur;
				cur = next;
			}
			return false;
		}
		size_t Size() const
		{
			return _n;
		}
		bool Empty() const
		{
			return Size() == 0;
		}
		size_t Count(const K& key)
		{
			Hash hs;
			size_t len = 0;
			size_t hashi = hs(key) % _tables.size();
			Node* cur = _tables[hashi];
			while (cur)
			{
				if (kot(cur->_data) == key)
				{
					len++;
				}
				cur = cur->_next;
			}
			return len;
		}
		size_t Bucket_count()
		{
			return _tables.size();
		}
		size_t Bucket_size(const K& key)
		{
			Hash hs;
			size_t len = 0;
			size_t hashi = hs(key) % _tables.size();
			Node* cur = _tables[hashi];
			while (cur)
			{
				len++;
				cur = cur->_next;
			}
			return len;
		}
	private:
		vector<Node*> _tables;
		size_t _n = 0;
	};
}